﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace tsgl
{
    public partial class departmentManage : Form
    {
        public departmentManage()
        {
            InitializeComponent();
        }

        private void initializeData()
        {
            DataTable dt = bookDbClass.ExecuteDataTable("select * from 部门");
            this.dataGridView1.DataSource = dt.DefaultView;
            this.dataGridView1.Columns[0].Width = 120;
            this.dataGridView1.Columns[4].Width = 120;
            this.txtDepartmentId.Text = this.dataGridView1[0, 0].Value.ToString();
            this.txtDepartmentName.Text = this.dataGridView1[1, 0].Value.ToString();
            this.txtDepartmentMan.Text = this.dataGridView1[2, 0].Value.ToString();
            this.txtContactMan.Text = this.dataGridView1[3, 0].Value.ToString();
            this.txtTel.Text = this.dataGridView1[4, 0].Value.ToString();
        }

        private string addressID;
        private void departmentManage_Load(object sender, EventArgs e)
        {
            initializeData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (addressID == "")
            {
                string str = "insert into 部门(部门编号,部门名称,部门负责人,联系人,联系电话) values(@a,@b,@c,@d,@e)";
                bookDbClass.ExecuteNonQurey(str, new SqlParameter("a", this.txtDepartmentId.Text.Trim()), new SqlParameter("b", this.txtDepartmentName.Text.Trim()), new SqlParameter("c", this.txtDepartmentMan.Text.Trim()), new SqlParameter("d", this.txtContactMan.Text.Trim()), new SqlParameter("e", this.txtTel.Text.Trim()));
            }
            else
            {
                if (this.txtDepartmentId.Text != addressID)
                {
                    try
                    {
                        string str = "update 部门 set 部门编号=@a,部门名称=@b,部门负责人=@c,联系人=@d,联系电话=@e where 部门编号=@f";
                        bookDbClass.ExecuteNonQurey(str, new SqlParameter("a", this.txtDepartmentId.Text.Trim()), new SqlParameter("b", this.txtDepartmentName.Text.Trim()), new SqlParameter("c", this.txtDepartmentMan.Text.Trim()), new SqlParameter("d", this.txtContactMan.Text.Trim()), new SqlParameter("e", this.txtTel.Text.Trim()), new SqlParameter("f", addressID));
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("更新失败！");
                    }
                }
                else
                {
                    string str = "update 部门 set 部门名称=@b,部门负责人=@c,联系人=@d,联系电话=@e where 部门编号=@f";
                    bookDbClass.ExecuteNonQurey(str, new SqlParameter("b", this.txtDepartmentName.Text.Trim()), new SqlParameter("c", this.txtDepartmentMan.Text.Trim()), new SqlParameter("d", this.txtContactMan.Text.Trim()), new SqlParameter("e", this.txtTel.Text.Trim()), new SqlParameter("f", this.txtDepartmentId.Text.Trim()));
                }
                initializeData();
                MessageBox.Show("修改成功！", "提示信息");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要删除该条数据吗！", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                string str = "delete from 部门 where 部门编号=@a";
                bookDbClass.ExecuteNonQurey(str, new SqlParameter("a", this.txtDepartmentId.Text.Trim()));
                initializeData();
                MessageBox.Show("删除成功！", "提示信息");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.txtDepartmentId.Text = this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
            this.txtDepartmentName.Text = this.dataGridView1[1, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
            this.txtDepartmentMan.Text = this.dataGridView1[2, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
            this.txtContactMan.Text = this.dataGridView1[3, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
            this.txtTel.Text = this.dataGridView1[4, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
            addressID = this.txtDepartmentId.Text;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            initializeData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}